﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cotrobaie
{
    public class NonPublic
    {
        public static Mesaje SuspendUser(int ID)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Useri, bool> fct = (DB.Useri u) => u.ID == ID;
                var usri = dbs.Useri.Where<DB.Useri>(fct);
                if (usri.Count<DB.Useri>()==0) return new Mesaje() { Success = false, Mesaj = "Userul cu ID-ul " + ID.ToString() + " nu exista in baza noastra de date" };
                var usr = usri.First<DB.Useri>();

                usr.AccountIsSuspended = true;
                dbs.SaveChanges();
                return new Mesaje() { Success = true, Mesaj = "Userul " + usr.Username + " a fost suspendat" };
            }
        }
        public static Mesaje UnSuspendUser(int ID)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Useri, bool> fct = (DB.Useri u) => u.ID == ID;
                var usri = dbs.Useri.Where<DB.Useri>(fct);
                if (usri.Count<DB.Useri>() == 0) return new Mesaje() { Success = false, Mesaj = "Userul cu ID-ul " + ID.ToString() + " nu exista in baza noastra de date" };
                var usr = usri.First<DB.Useri>();

                usr.AccountIsSuspended = false;
                dbs.SaveChanges();
                return new Mesaje() { Success = true, Mesaj = "Userului " + usr.Username + " i-a fost ridicata suspendarea" };
            }
        }
        public static Mesaje DeleteUser(int ID)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Useri, bool> fct = (DB.Useri u) => u.ID == ID;
                var usri = dbs.Useri.Where<DB.Useri>(fct);
                if (usri.Count<DB.Useri>() == 0) return new Mesaje() { Success = false, Mesaj = "Userul cu ID-ul " + ID.ToString() + " nu exista in baza noastra de date" };
                var usr = usri.First<DB.Useri>();

                usr.AccountIsTempDeleted = true;
                usr.AccountTempDelDate = DateTime.Now;
                dbs.SaveChanges();
                return new Mesaje() { Success = true, Mesaj = "Userul " + usr.Username + " a fost sters" };
            }
        }
        public static DB.Useri GetUser(string Username)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Useri, bool> fct = (DB.Useri u) => u.Username.ToLower() == Username.ToLower();
                var usri = dbs.Useri.Where<DB.Useri>(fct);
                if (usri.Count<DB.Useri>() == 1)
                {
                    return usri.First<DB.Useri>();
                }
            }
            return null;
        }
        public static DB.Useri GetUserByEmail(string Email)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Useri, bool> fct = (DB.Useri u) => u.E_mail.ToLower() == Email.ToLower();
                var usri = dbs.Useri.Where<DB.Useri>(fct);
                if (usri.Count<DB.Useri>() == 1)
                {
                    return usri.First<DB.Useri>();
                }
            }
            return null;
        }

        public static Mesaje AddAdmin(string Username, string Parola)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Admini, bool> fct = (DB.Admini u) => u.Username.ToLower() == Username.ToLower();
                if (dbs.Admini.Where<DB.Admini>(fct).Count<DB.Admini>() > 0)
                    return new Mesaje() { Success = false, Mesaj = "Ne pare rau dar " + Username + " este deja folosit" };
                try
                {
                    dbs.AddToAdmini(new DB.Admini() { Username = Username, Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Parola, "MD5"), DataInregistrare = DateTime.Now, DataUltimaLogare = DateTime.Now });
                    dbs.SaveChanges();
                    return new Mesaje() { Success = true, Mesaj = Username + " a fost adaugat cu succes" };
                }
                catch
                { return new Mesaje() { Success = false, Mesaj = "Ne pare rau dar a aparut o eroare in momentul adaugarii. Va rugam mai incercati odata" }; }
            }
        }
        public static Mesaje EditAdmin(string Username, string Parola)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Admini, bool> fct = (DB.Admini u) => u.Username.ToLower() == Username.ToLower();
                var admi = dbs.Admini.Where<DB.Admini>(fct);
                if (admi.Count<DB.Admini>()==0)
                    return new Mesaje() { Success = false, Mesaj = Username + " nu exista in baza noastra de date" };
                var adm = admi.First<DB.Admini>();
                try
                {
                    adm.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Parola, "MD5");
                    dbs.SaveChanges();
                    return new Mesaje() { Success = true, Mesaj = Username + " a fost editat cu succes" };
                }
                catch
                { return new Mesaje() { Success = false, Mesaj = "Ne pare rau dar a aparut o eroare in momentul editarii informatiilor. Va rugam mai incercati odata" }; }
            }
        }
        public static Mesaje DeleteAdmin(string Username)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Admini, bool> fct = (DB.Admini u) => u.Username.ToLower() == Username.ToLower();
                var admi = dbs.Admini.Where<DB.Admini>(fct);
                if (admi.Count<DB.Admini>()==0)
                    return new Mesaje() { Success = false, Mesaj = Username + " nu exista in baza noastra de date" };
                var adm = admi.First<DB.Admini>();
                try
                {
                    dbs.Admini.DeleteObject(adm);
                    dbs.SaveChanges();
                    return new Mesaje() { Success = true, Mesaj = Username + " a fost editat cu succes" };
                }
                catch
                { return new Mesaje() { Success = false, Mesaj = "Ne pare rau dar a aparut o eroare in momentul stergerii adminului. Va rugam mai incercati odata" }; }
            }
        }
        public static Mesaje LoginAdmin(string Username, string Parola)
        {
            using (var dbs = new DB.DBCotrobaie())
            {
                Func<DB.Admini, bool> fct = (DB.Admini u) => u.Username.ToLower() == Username.ToLower();
                var admi = dbs.Admini.Where<DB.Admini>(fct);
                if (admi.Count<DB.Admini>() == 0)
                {
                    UserIpuriBanate();
                    return new Mesaje() { Success = false, Mesaj = Username + " nu exista in baza noastra de date" };
                }
                var adm = admi.First<DB.Admini>();
                try
                {
                    if (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Parola, "MD5").ToUpper() == adm.Password.ToUpper())
                    {
                        adm.DataUltimaLogare = DateTime.Now;
                        dbs.SaveChanges();
                        return new Mesaje() { Success = true, Mesaj = "Login efectuat cu succes" };
                    }
                    else
                    {
                        UserIpuriBanate();
                        return new Mesaje() { Success = false, Mesaj = "Parola incorecta" };
                    }
                }
                catch
                { return new Mesaje() { Success = false, Mesaj = "Ne pare rau dar a aparut o eroare in momentul logarii adminului. Va rugam mai incercati odata" }; }
            }
        }


        public static int UserIpuriBanate()
        {
            string IP = String.Empty;
            try { IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; }
            catch { return 0; }
            using (var dbs = new DB.DBCotrobaie())
            {
                var usr = from v in dbs.IpuriBanate where v.IP.StartsWith(IP) && v.IP.Length == IP.Length select v;
                if (usr.Count<DB.IpuriBanate>() == 1)
                {
                    if (usr.First<DB.IpuriBanate>().Data < DateTime.Now.AddDays(-2))
                    {
                        usr.First<DB.IpuriBanate>().NrFailed = 0;
                        dbs.SaveChanges();
                        return 0;
                    }
                    return usr.First<DB.IpuriBanate>().NrFailed;
                }
                else return 0;
            }
        }
    }
}